#pragma once

//
// Helper class to manage locking on any object
// that supports "Lock" and "Unlock" methods.
// Locks the object in the constructor and unlocks
// it in the destructor.  It is expected that instances
// of this class will be created only on the stack.
//
template<typename T>
class CObjectLockT
{
private:
	T *m_pObject;

public:
	CObjectLockT(const T *pObject) : m_pObject(const_cast<T *>(pObject))
	{
		m_pObject->Lock();
	}

	~CObjectLockT(void)
	{
		m_pObject->Unlock();
	}
};

template<typename T>
class CAutoCriticalSectionLockable
{
protected:
	CComAutoCriticalSection m_cs;
	typedef CObjectLockT<T> CObjectLock;

public:
	virtual void Lock()
	{
		m_cs.Lock();
	}

	virtual void Unlock()
	{
		m_cs.Unlock();
	}
};
